package com.evildog.workspace.file.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.file.bo.DocBO;
import com.evildog.workspace.file.bo.DocPageBO;
import com.evildog.workspace.file.convert.DocConvert;
import com.evildog.workspace.file.dto.DocDTO;
import com.evildog.workspace.file.dto.DocPageDTO;
import com.evildog.workspace.file.entity.Doc;
import com.evildog.workspace.file.entity.DocPageDO;
import com.evildog.workspace.file.mapper.DocMapper;
import com.evildog.workspace.file.service.DocService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class DocServiceImpl extends ServiceImpl<DocMapper, Doc> implements DocService {

    @Override
    public PagedResult<DocPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, DocPageDTO pageDTO) {
        IPage<DocPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                DocConvert.INSTANCE.convert(pageDTO));
        return DocConvert.INSTANCE.convert(page);
    }

    @Override
    public List<DocBO> getList(List<QueryConditionDTO> queryCondition, DocDTO dto) {
        List<Doc> docList = this.baseMapper.getList(queryCondition, DocConvert.INSTANCE.convert(dto));
        return DocConvert.INSTANCE.convert(docList);
    }

    @Override
    public DocBO getDetailById(Long id) {
        Doc doc = this.getById(id);
        Assert.notNull(doc, "doc is null!");
        return DocConvert.INSTANCE.convert(doc);
    }

    @Override
    public boolean save(DocDTO dto){
        Doc newDoc = DocConvert.INSTANCE.convert(dto);
        return save(newDoc);
    }

    @Override
    public boolean updateById(Long id, DocDTO dto){
        Doc oldDoc = getById(id);
        Assert.notNull(oldDoc, "doc is null!");
        Doc newDoc = DocConvert.INSTANCE.convert(dto);
        newDoc.setId(id);
        return updateById(newDoc);
    }
}
